package Controller;

import Data.*;
import MyInterfaces.MassageType;
import Service.Main;
import Service.SendMassage;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class InviteOtherToGroup {

    @FXML
    private ListView<PrivateInformationData> friendlist;

    @FXML
    private Button invite;

    final PrivateInformationData[] frienddata = new PrivateInformationData[1];

    private String group_id;

    private GroupData groupData;

    public void setGroupData(GroupData groupData)
    {
        this.groupData=groupData;
    }

    public void setStage(ArrayList<PrivateInformationData> arrayList, HashMap<String, GroupFriendData> friendmap)
    {
        ArrayList<GroupFriendData> list=new ArrayList<>();
        System.out.println(arrayList.size()+" "+friendmap.size());
        ArrayList<PrivateInformationData> newArry=new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            if(friendmap.containsKey(arrayList.get(i).getUserid()))
            {
                System.out.println(friendmap.get(arrayList.get(i).getUserid()).getId());
                System.out.println(arrayList.get(i).getUserid());
                //如果有不显示
            }
            else
            {
                newArry.add(arrayList.get(i));
            }
        }
        frienddata[0]=null;

        ObservableList<PrivateInformationData> oblist= FXCollections.observableArrayList();
        friendlist.setItems(oblist);
        oblist.setAll(newArry);
        friendlist.refresh();

        System.out.println(newArry.size());

        friendlist.setCellFactory(new Callback<ListView<PrivateInformationData>, ListCell<PrivateInformationData>>() {
            @Override
            public ListCell<PrivateInformationData> call(ListView<PrivateInformationData> param) {
                ListCell<PrivateInformationData> list=new ListCell<PrivateInformationData>(){
                    @Override
                    protected void updateItem(PrivateInformationData arg0, boolean arg1) {
                        super.updateItem(arg0, arg1);
                        if(arg1==false)
                        {
                            //显示头像和昵称
                            setPrefHeight(70);
                        //    setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
                            HBox hbox=new HBox(10);
                            hbox.setAlignment(Pos.BASELINE_LEFT);//对齐显示
                            Image image=new Image(new ByteArrayInputStream(arg0.getAvatar()));
                            ImageView imageView=new ImageView(image);
                            Label Id=new Label(arg0.getUserid());
                            imageView.setFitWidth(60);
                            imageView.setFitHeight(60);
                            Circle circle=new Circle(30,30,30);
                            Label la=new Label(arg0.getUserNickName());
                            la.setFont(Font.font(15));
                            imageView.setClip(circle);
                            hbox.getChildren().addAll(imageView,Id,la);//添加
                            this.setGraphic(hbox);//启动
                        }
                        else
                        {
                            setPrefHeight(0);
                        }

                    }

                };
                return list;
            }
        });
        friendlist.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<PrivateInformationData>() {
            @Override
            public void changed(ObservableValue<? extends PrivateInformationData> observableValue, PrivateInformationData s, PrivateInformationData t1) {
                if(t1!=null)
                {
                    frienddata[0] =t1;
                }
                else return;
            }
        });
    }

    @FXML
    void inviteAction(ActionEvent event) throws IOException {
        if(frienddata[0]!=null)
        {
            //向服务器发送请求
            event massage=new event();
            massage.setType(MassageType.Invite_Other_To_Group);
            //新建一个消息类
            GroupOperationData groupOperationData=new GroupOperationData();
            groupOperationData.setSender(groupData.getId());
            //设置好友的信息
            GroupFriendData groupFriendData=new GroupFriendData();
            groupFriendData.setGroup_id(groupData.getGroup_id());
            groupFriendData.setUserNickName(frienddata[0].getUserNickName());
            groupFriendData.setAvatar(frienddata[0].getAvatar());
            groupFriendData.setId(frienddata[0].getUserid());

            groupOperationData.setReceiver(groupFriendData);

            //设置群聊列表
            GroupStage groupStage=Main.AllContronller.getGroupStage(groupData.getGroup_id());
            System.out.println(groupData.getGroup_id());
            ArrayList<GroupFriendData> arrayList=groupStage.getFriendArryList();
            groupOperationData.setFriendlist(arrayList);

            massage.setT(groupOperationData);

            new SendMassage().sendMassage(massage);
            System.out.println("发送请求成功");
        }
    }

    @FXML
    void closeAction(ActionEvent event) {
        Stage stage=(Stage) invite.getScene().getWindow();
        stage.close();
        Main.AllContronller.RemoveInviteToOtherController(groupData.getGroup_id());
    }

}
